var express = require('express');
var router = express.Router();
var sendCode = require('./../tool/sendCode');
var md5 = require('md5');
var sql = require('./../tool/sql');
var UserModel = require('../tool/model/clientUser.js');
var cookieParser = require('cookie-parser');
// function randomCode () {
//     var num = Math.floor(Math.random() * 1000 ) + 1000
//     return num
// }
const randomCode = () => Math.floor(Math.random() * 1000 ) + 1000

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
router.post('/register',async function(req, res, next) {
    let obj = req.body; // {username: '', password: ''}
    obj.password = md5(obj.password);
    console.log(obj)
   await UserModel.find({'userEmail':obj.email},(err,data)=>{
       if(err){
           console.log(err)
           return
       }else{
        if (data.length == 0){
            var user = new UserModel ({
                userName:obj.name,
                userPassword:obj.password,
                userEmail:obj.email,//以邮箱作为该用户的管理标识，若更该则更新此字段
                userPhone:obj.phone,
                desc:obj. desc
              })
               user.save(function(err,doc){
                if(err){
                    console.log(err);
                    return;
                }else{
                    res.send({
                            code: 200,
                            message: '注册成功',
                            data: 1,
                            type:'success'
                        })
                }
            })
        }else{
            res.send({
                    code: 200,
                    message: '该邮箱已注册',
                    data: 0,
                    type:'error'
                    })
        }
       }
    })
});
router.post('/login', function(req, res, next) {
    let obj = req.body; // {email: '', password: ''}
    obj.password = md5(obj.password);
    console.log(req)
    // 验证有没有该用户
    UserModel.findOne({'userEmail':obj.email},(err,datas)=>{
        if(err){
            res.send({
                code: 200,
                message: '登录出错',
                data: 0,
                type:'error'
            })
        }
        if (datas.length == 0) { // 没有该用户
            res.send({
                code: 200,
                message: '没有该用户的信息',
                data: 0,
                type:'error'
            })
        }else{ 
            console.log(datas.userPassword)
            console.log(datas.userPassword == obj.password )
            if(datas.userPassword == obj.password  ){
                console.log(datas)
            res.send({
                code: 200,
                message: '登录成功',
                data: datas,
                type:'success'
            })
            }else{
                res.send({
                    code: 200,
                    message: '密码错误',
                    data: 0,
                    type:'error'
                })
            }
        }
    })
});

router.post('/sendCode', function(req, res, next) {
    const obj = req.body; // {tel: 18813007814}
    const num = randomCode(); 
    console.log(obj)
    UserModel.find({'userEmail':obj.email},(err,data)=>{
        if (data.length == 0) { // 没有注册，可以发送验证码
            sendCode.send(obj.phone, num).then(() => {
                res.send({
                    code: 200,
                    message: '验证码正在发送，注意查收',
                    data: num,
                    type:'success'
                })
            }).catch(err => {
                res.send({
                    code: 200,
                    message: 'error'
                })
            })
        } else {
            res.send({
                code: 200,
                message: '该用户已注册',
                data: 2,
                type:'error'
            })
        }
    })
   
    
  });

module.exports = router;
